package com.lengxf.pay.wx.service;

import com.lengxf.pay.common.bean.PayOrder;
import com.lengxf.pay.wx.config.WX_PayConfig;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;

public record WxNativePayService(Config config, WX_PayConfig wxPayConfig) {


    public String qrPay(PayOrder payOrder) {
        NativePayService jsapiService = new NativePayService.Builder().config(config).build();
        PrepayResponse prepay = jsapiService.prepay(buildRequest(payOrder));
        return prepay.getCodeUrl();
    }

    private PrepayRequest buildRequest(PayOrder payOrder) {
        PrepayRequest request = new PrepayRequest();
        request.setAppid(wxPayConfig.getAppid());
        request.setMchid(wxPayConfig.getMerchantId());
        request.setDescription(payOrder.getProductDesc());
        request.setOutTradeNo(payOrder.getOrderNo());
        request.setNotifyUrl(wxPayConfig.notifyUrl());
        //构建金额数据
        Amount amount = new Amount();
        amount.setTotal(payOrder.getPrice().intValue());
        request.setAmount(amount);
        return request;
    }

}
